package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.AdminDao;
import Entity.Admin;

public class LoginServlet extends HttpServlet {

    private AdminDao admindao = new AdminDao();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String path = "Hello";
        List<Admin> list = admindao.select();
        for (Admin u : list) {
            if (u.getUsername().equals(request.getParameter("username")) && u.getPassword().equals(request.getParameter("password"))) {
                request.getSession().setAttribute("username", u.getUsername());//登录成功，将用户数据放入到Session中
                request.getSession().setAttribute("username_code", u.getCode());
                response.setHeader("Pragma", "No-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                request.getRequestDispatcher(path).forward(request, response);
                return;
            }
        }
        request.getRequestDispatcher("/pages/Login_Station.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
